package com.songlanyun.modules.shop.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.modules.exception.ShopException;
import com.songlanyun.modules.shop.entity.ShopApplyEntity;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.model.dto.ShopApplyDTO;
import com.songlanyun.modules.shop.model.dto.ShopDTO;
import com.songlanyun.modules.shop.model.dto.ShopUpdateRecordDTO;
import com.songlanyun.modules.shop.service.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.shop.dao.ShopUpdateRecordDao;
import com.songlanyun.modules.shop.entity.ShopUpdateRecordEntity;
import com.songlanyun.modules.shop.service.ShopUpdateRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("shopUpdateRecordService")
public class ShopUpdateRecordServiceImpl extends ServiceImpl<ShopUpdateRecordDao, ShopUpdateRecordEntity> implements ShopUpdateRecordService {

    @Resource
    private ShopService shopService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long shopId = MapUtil.getLong(params, "shop_id");
        String shopName = MapUtil.getStr(params, "shop_name");
        Integer auditStatus = MapUtil.getInt(params, "audit_status");
        Boolean unionBusiness = MapUtil.getBool(params, "union_business");//是否是联盟商家
        String provinceCode = MapUtil.getStr(params, "province_code");
        String cityCode = MapUtil.getStr(params, "city_code");
        String areaCode = MapUtil.getStr(params, "area_code");

        IPage<ShopUpdateRecordEntity> page = this.page(
                new Query<ShopUpdateRecordEntity>().getPage(params),
                new LambdaQueryWrapper<ShopUpdateRecordEntity>()
                        .eq(shopId != null, ShopUpdateRecordEntity::getShopId, shopId)
                        .like(StringUtils.isNotBlank(shopName), ShopUpdateRecordEntity::getShopName, shopName)
                        .eq(auditStatus != null, ShopUpdateRecordEntity::getAuditStatus, auditStatus)
                        .eq(ShopUpdateRecordEntity::getUnionBusiness, ObjectUtil.isNull(unionBusiness) ? 0 : unionBusiness)//是否是联盟商家

                        .eq(StringUtils.isNotBlank(provinceCode), ShopUpdateRecordEntity::getProvinceCode, provinceCode)
                        .eq(StringUtils.isNotBlank(cityCode), ShopUpdateRecordEntity::getCityCode, cityCode)
                        .eq(StringUtils.isNotBlank(areaCode), ShopUpdateRecordEntity::getAreaCode, areaCode)

                        .orderByDesc(ShopUpdateRecordEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecord(ShopUpdateRecordDTO dto, Long shopId) {
        //一个商家同时只能有一个申请
        verifyRecordOne(shopId);

        //查询该商家是否存在
        ShopEntity shop = shopService.getById(shopId, true);

        //保存
        ShopUpdateRecordEntity record = new ShopUpdateRecordEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto, record);
        record.setShopId(shop.getId());
        record.setAuditStatus(Constant.AUDITWAIT);//待审核
        record.setUnionBusiness(shop.getUnionBusiness());

        this.baseMapper.insert(record);

    }

    @Override
    @Transactional
    public void updateRecord(ShopUpdateRecordEntity record) {
        //保存审核信息
        ShopUpdateRecordEntity entity = this.getById(record.getId(), true);
        entity.setAuditStatus(record.getAuditStatus());
        entity.setAuditRemark(record.getAuditRemark());
        entity.setAuditTime(new Date());
        this.baseMapper.updateById(entity);

        //审核成功，则更新商家信息
        if (Constant.AUDITPASS == entity.getAuditStatus()) {
            ShopEntity shop = shopService.getById(entity.getShopId(), true);
            BeanUtil.copyPropertiesIgnoreNull(entity, shop);
            shop.setId(entity.getShopId());
            shopService.updateById(shop);
        }

    }

    @Override
    public ShopUpdateRecordEntity getById(Long id, Boolean isThrow) {
        ShopUpdateRecordEntity record = this.baseMapper.selectById(id);
        if (record == null && isThrow) {
            throw new RRException(ShopException.SHOP_UPDATE_NOT_EXIST);
        }
        return record;
    }

    private void verifyRecordOne(Long shopId) {
        Integer count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<ShopUpdateRecordEntity>()
                        .eq(ShopUpdateRecordEntity::getShopId, shopId)
                        .eq(ShopUpdateRecordEntity::getAuditStatus, Constant.AUDITWAIT)
        );
        if (count > 0) {
            throw new RRException(ShopException.SHOP_UPDATE_AUDIT_WAIT);
        }
    }

}
